import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/config/app_config.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/utils/utils_image.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';

import 'promotion_link_logic.dart';

class PromotionLinkPage extends StatelessWidget {
  PromotionLinkPage({super.key});

  final PromotionLinkLogic logic = Get.put(PromotionLinkLogic());
  final GlobalKey _repaintBoundaryKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<PromotionLinkLogic>(
      // appBar: CommonAppBar(title: 'title'),
      builder: (_) {
        return Stack(
          children: [
            // 隐藏的保存样式Widget（用于截图）
            Positioned(
              left: -10000, // 移到屏幕外
              child: _buildSaveImageWidget(),
            ),
            Column(
              children: [
                Container(
              margin: EdgeInsets.all(16.w),
              decoration: BoxDecoration(
                  color: AppColor.color070402,
                  borderRadius: BorderRadius.circular(12.w)),
              padding: EdgeInsets.symmetric(vertical: 24.w),
              child: Column(
                children: [
                  Text(
                    '${LocaleKeys.inviteJoin.tr}U9哈希',
                    style: TextStyle(
                      color: AppColor.primary,
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w900,
                    ),
                  ),
                  12.verticalSpaceFromWidth,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                     LocaleKeys.yourInviteCode.tr ,
                        style: TextStyle(
                          color: Colors.white.withOpacity(0.94),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w900,
                        ),
                      ),
                      10.horizontalSpace,
                      Padding(
                        padding: EdgeInsets.symmetric(
                            horizontal: 12.w, vertical: 10.w),
                        child: Text(
                          AppUserManager().user.value?.inviteCode ?? '',
                          style: TextStyle(
                            color: Colors.white.withOpacity(0.94),
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w900,
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 198.w,
                    width: double.infinity,
                    child: Stack(
                      children: [
                        // 背景图片
                        Container(
                          height: 198.w,
                          width: double.infinity,
                          decoration: const BoxDecoration(
                            image: DecorationImage(
                                image: AssetImage(Assets.promotionCommissionLinkBg),
                                fit: BoxFit.fill),
                          ),
                        ),
                        // 居中的二维码
                        Center(
                          child: Container(
                            width: 150.w,
                            height: 150.w,
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.17),
                              borderRadius: BorderRadius.circular(12.w)
                            ),
                            alignment: Alignment.center,
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(12.w),
                              child: QrImageView(
                                data: '${AppConfig.shareUrl}${AppConfig.shareUrlCenter}${AppUserManager().user.value?.inviteCode ?? ''}',
                                version: QrVersions.auto,
                                size: 138.w,
                                backgroundColor: Colors.white,
                              ),
                            )
                          )
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                      onTap: () {
                        logic.copyShareLink();
                      },
                      child: Container(
                        margin: EdgeInsets.symmetric(horizontal: 12.w),
                        padding: EdgeInsets.only(
                            top: 10.w, bottom: 10.w, right: 6.w, left: 12.w),
                        decoration: BoxDecoration(
                            color: AppColor.pageBgColor,
                            borderRadius: BorderRadius.circular(8.w),
                            border: Border(
                                bottom: BorderSide(
                              width: 1,
                              color: AppColor.textPrimary.withOpacity(0.15),
                            ))),
                        child: Row(
                          children: [
                            Expanded(
                                child: Text(
                                  // https://web.798ys.xyz/login?action=register&invite_code=ZUN44M3
                              '${AppConfig.shareUrl}${AppConfig.shareUrlCenter}${AppUserManager().user.value?.inviteCode ?? ''}',
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: AppColor.textPrimary.withOpacity(0.94),
                              ),
                            )),
                            12.horizontalSpace,
                            AutoImage(
                              Assets.gameCopy,
                              width: 32.w,
                            ),
                          ],
                        ),
                      )),
                  32.verticalSpaceFromWidth,
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 12.w),
                    child: Row(
                      children: [
                        Expanded(
                            child: CommonButton(
                          title: LocaleKeys.saveImage.tr,
                          enable: false,
                          alwaysCanClick: true,
                          fontSize: 14.sp,
                          onClick: () {
                            UtilImage.saveImageToGallery(_repaintBoundaryKey);
                            // logic.saveImageToGallery(_repaintBoundaryKey);
                          },
                        )),
                        12.horizontalSpace,
                        Expanded(
                            child: CommonButton(
                          title: LocaleKeys.copyLink.tr,
                          fontSize: 14.sp,
                          onClick: () {
                            logic.copyShareLink();
                          },
                        )),
                      ],
                    ),
                  )
                ],
              ),
            )
              ],
            ),
          ],
        );
      },
    );
  }

  /// 构建保存图片样式的Widget（按照上传的图片样式设计）
  Widget _buildSaveImageWidget() {
    return RepaintBoundary(
      key: _repaintBoundaryKey,
      child: Container(
        width: 375.w, // 固定宽度，适合保存
        height: 500.w, // 固定高度
        color: Colors.black, // 黑色背景
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 标题文字
            Text(
              '${LocaleKeys.inviteJoin.tr}${AppConfig.appName}',
              style: TextStyle(
                color: AppColor.primary, // 亮蓝色
                fontSize: 28.sp,
                fontWeight: FontWeight.w900,
              ),
            ),
            40.verticalSpaceFromWidth,
            // 邀请码文字
            Text(
              '${LocaleKeys.yourInviteCode.tr} ${AppUserManager().user.value?.inviteCode ?? ''}',
              style: TextStyle(
                color: Colors.white, // 白色
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            60.verticalSpaceFromWidth,
            // 带边框的二维码
            Container(
              width: 150.w,
              height: 150.w,
              decoration: BoxDecoration(
                color: Colors.white, // 白色背景
                borderRadius: BorderRadius.circular(12.w),
                border: Border.all(
                  color: Colors.grey[600]!, // 深灰色描边
                  width: 2.w,
                ),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.1),
                    blurRadius: 8.w,
                    offset: Offset(0, 4.w),
                  ),
                ],
              ),
              padding: EdgeInsets.all(8.w),
              child: QrImageView(
                data: '${AppConfig.shareUrl}${AppConfig.shareUrlCenter}${AppUserManager().user.value?.inviteCode ?? ''}',
                version: QrVersions.auto,
                size: 130.w,
                backgroundColor: Colors.white,
                foregroundColor: Colors.black,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
